<?php

namespace app\api\controller;

use app\admin\model\Cityhave;
use app\admin\model\DoMember;
use app\admin\model\Expert;
use app\admin\model\Projectgongsi;
use app\admin\model\Projectkefu;
use app\admin\model\Projecttype;
use app\admin\model\Projectzixun;
use app\admin\model\Shoprogoods;
use app\admin\model\Wenhuahave;
use app\admin\model\yalimedia\Teacher;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function wenchuang()
    {
        $map = [
            'is_master'=>1,
            'utype' => 0
        ];
       /* if(area_id()){
            $map['area_id'] = ['in',area_id()];
        }*/
        $teacher = new Teacher();
        if(area_ids()){
            $teacher->where(area_ids());
        }
        $list = $teacher->where($map)->limit(4)->select();
        $teacher = collection($list)->toArray();
        foreach($teacher as &$v){
            $v['image'] =  cdnurl($v['image'],true);
        }
        $map = [
            'status' => 1
        ];
        $gongsi = new Projectgongsi();
        if(area_ids()){
            $gongsi->where(area_ids());
        }
        $list = $gongsi->where($map)->limit(4)->order('id desc')->select();
        $company = collection($list)->toArray();
        foreach($company as &$v1){
            $v1['image'] =  cdnurl($v1['image'],true);
        }
        $data = [
            'teacher'=>$teacher,
            //'company' => $company,
        ];
        $this->success('成功',$data);
    }

    public function teach()
    {
        $map = [];
        if(input('search')){
            $map['name'] = ['like','%'.input('search').'%'];
        }else{
            $type = input('type',0);
            $map = [
                'utype' => $type
            ];
        }
        if(input('cate_id')){
            $smap = [
                'status' => 1,
            ];
            $mlist = Db::table('fa_yalimedia_teacher_cate')
                ->where('pid',input('cate_id'))->where($smap)
                ->select();
            if($mlist){
                $map['cate_id']= ['in',array_column($mlist,'id')];
            }else{
                $map['cate_id']=  input('cate_id');
            }
        }
        /*if(area_id()){
            $map['area_id'] = ['in',area_id()];
        }*/
        $Teacher = new Teacher();
        if(area_ids()){
            $Teacher->where(area_ids());
        }
        $list = $Teacher->where($map)->paginate()->toArray();
        foreach ($list['data'] as &$v){
            $v['image'] =  cdnurl($v['image'],true);
            $v['tags'] =  $v['tags']?explode(',',$v['tags']):[];

        }
        $this->success('成功',$list);
    }

    public function hotlist()
    {
        $map = [
            'is_hot' => 1,
        ];
        $list = Teacher::where($map)->limit(6)->select();
        foreach ($list as &$v){
            $v['image'] =  cdnurl($v['image'],true);
        }
        $this->success('成功',$list);
    }

    public function detail()
    {
        $id = input('id');
        if(empty($id)){
            $this->error('id必传');
        }
        $teach = Teacher::get($id);
        if(empty($teach)){
            $this->error('老师不存在');
        }
        $teach = $teach->toArray();
        $teach['job'] = $teach['job']?$teach['job']:'';
        $teach['image'] = cdnurl($teach['image'],true);

        $map = [
            'teacher_id'=>$id,
            'status'=>1,
            'is_check' => 1,
        ];
        $num = DoMember::where($map)->count();
        $teach['num'] = $num;
        $map = [
            'teacher_id'=>$id,
            'status'=>1,
            'user_id'=>$this->auth->id,
            'is_check' => 1,
        ];
        $do = DoMember::where($map)->find();
        $teach['is_jingji'] = $do?1:0;

        $cate = Db::table('fa_yalimedia_course_category')
            ->where('status','normal')
            ->field('id as cate_id,name')
            ->select();
        $arr = ['cate_id'=>'','name' => '全部'];
        array_unshift($cate,$arr);
        $map = [
            'status' => 1,
            'teacher_id' => $id
        ];
        if(input('cate_id')){
            $map += [
                'cate_id' => input('cate_id'),
            ];
        }
        $course = Db::table('fa_yalimedia_course')
            ->where($map)
            ->field('id,title,cover,content,price')
            ->select();
        $course = collection($course)->toArray();
        foreach ($course as &$v){
            $v['cover'] = cdnurl($v['cover'],true);
            $map = [
                'user_id' => $this->auth->id,
                'item_id' => $v['id'],
                'pay_status' => 1,
            ];
            $order = Db::table('fa_yalimedia_order')->where($map)->find();
            $v['is_buy'] =  $order?1:0;//0=未购买，1=已购买
            $is_fav = 0;
            if($this->auth->isLogin()){
                $map = [
                    'user_id' => $this->auth->id,
                    'item_id' => $v['id'],
                ];
                $c = Db::table('fa_yalimedia_fav')->where($map)->find();
                $is_fav = $c?1:0;
            }
            $v['is_fav'] = $is_fav;//0=未收藏，1=已收藏
        }
        $act = Projectzixun::where("ids",$id)->order('id desc')->find();
        if($act){
            $act['image'] = cdnurl($act['image'],true);
            $act['project_content'] = strip_tags($act['project_content']);
            $act['time_text'] = date('Y.m.d H:i',$act['createtime']);

        }
        $map = [
            'teacher_id'=>$id,
            'status'=>1,
            'is_check' => 1,
        ];
        $jingji_list = DoMember::where($map)->field('id,user_id')->order('id desc')->limit(10)->select();
        foreach ($jingji_list as &$vc){
            $user = \app\admin\model\User::get($vc['user_id']);
            $vc['nickname'] = $user?$user['nickname']:'';
            $vc['avatar'] =  $user?cdnurl($user['avatar'],true):'';
        }
        $data = [
            'teach'=>$teach,
            'jingji_list'=> $jingji_list,
            'act' => $act,
            'cate' => $cate,
            'course' => $course,
        ];
        $this->success('成功',$data);
    }

    //活动列表
    public function actlist()
    {
        $map = [
            'status' => 1,
        ];
        if(input('teacher_id')){
            $map+=[
                'ids' => input('teacher_id')
            ];
        }
        $list = Projectzixun::where($map)->paginate()->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],true);
            $v['project_content'] = strip_tags($v['project_content']);
            $v['time_text'] = date('Y.m.d H:i',$v['createtime']);
        }
        $this->success('成功',$list);
    }

    public function actdetail()
    {
        $act = Projectzixun::where('id',input('id'))->order('id desc')->find();
        if($act){
            $act['image'] = cdnurl($act['image'],true);
            $act['time_text'] = date('Y.m.d H:i',$act['createtime']);
            $this->success('成功',$act);
        }else{
            $this->error('失败');
        }
    }

    public function wenchuanhave()
    {
        if(empty(input('project_name'))){
            $this->error('project_name必传');
        }
        $user = $this->auth->getUser();
        $input = input();
        $data['user_id'] = $user['id'];
        $data['project_name'] = $input['project_name'];
        $data['project_desc'] = $input['project_desc'];
        $data['file'] = $input['file'];
        $model = new Wenhuahave($data);
        $model->allowField(true)->save();
        $this->success('成功');
    }

    public function gongsilist()
    {
        $map = [
            'status' => 1,
        ];
        /*if(area_id()){
            $map['area_id'] = ['in',area_id()];
        }*/
        if((int)input('cate_id')){
            $smap = [
                'status' => 1,
            ];
            $mlist = Db::table('fa_yalimedia_teacher_cate')
                ->where('pid',input('cate_id'))->where($smap)
                ->select();
            if($mlist){
                $map['cate_id']= ['in',array_column($mlist,'id')];
            }else{
                $map['cate_id'] = (int)input('cate_id');
            }
        }
        if(input('search')){
            $map['project_name'] = ['like','%'.input('search').'%'];
        }
        $Projectgongsi = new Projectgongsi();
        if(area_ids()){
            $Projectgongsi->where(area_ids());
        }
        $list = $Projectgongsi->where($map)->order('id desc')->paginate()->toArray();
        foreach ($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],true);
        }
        $this->success('成功',$list);
    }

    public function gongsi()
    {
        $id = input('id');
        $project = new \app\admin\model\Projectgongsi();

        $info = $project->where('id', $id)->find();
        if(empty($info)){
            $this->error('参数错误');
        }
        $info = $info->toArray();
        $info['file'] = cdnurl($info['file'],true);

        $res = file_get_contents('https://apis.map.qq.com/ws/geocoder/v1/?address='.$info['address'].'&key=34GBZ-ZG66D-KV54R-P3DBA-L5F6O-QQFOT');
        $info['location'] = [];
        $res = json_decode($res,true);

        if($res['message'] == 'Success'){
            if(!empty($res['result']['location'])){
                $info['location'] = $res['result']['location'];
            }
        }


        $expert_list = Expert::where('id','in',explode(',',$info['expert_ids']))->select();
        $expert_list = collection($expert_list)->toArray();
        foreach($expert_list as &$vv){
            $vv['image'] = cdnurl($vv['image'],true);
        }
        $info['expert_list'] = $expert_list;
        $expert_id = 0;
        if($expert_list){
            $expert_id = $expert_list[0]['id'];
        }
        $expert = Expert::where('id',$expert_id)->find();
        $expert['image'] = cdnurl($expert['image'],true);
        $info['expert'] = $expert;
        $info['is_two'] = count($expert_list)>1?1:0;
        $goodsList = Shoprogoods::where('id','in',explode(',',$info['goods_ids']))
            ->field('id,title,subtitle,image')
            ->select();
        foreach ($goodsList as &$v){
            $v['image'] = cdnurl($v['image'],true);
        }
        $info['goodslist'] = $goodsList;

        $this->success(null, $info);

    }

    public function typelist()
    {
        $list = Projecttype::where('status',1)->select();
        $this->success('成功', $list);
    }

}
